package com.yl.nettylearning.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author yangl
 */
public class NewIOServer {
    public static void main(String[] args) throws IOException {
        //开启一个端口
        InetSocketAddress address = new InetSocketAddress(7001);
        //创建服务器端连接
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket socket = serverSocketChannel.socket();
        //绑定端口
        socket.bind(address);
        //创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        while (true){
            SocketChannel socketChannel = serverSocketChannel.accept();
            int readCount = 0;
            while (-1 != readCount){
                try {
                    readCount = socketChannel.read(byteBuffer);
                }catch (Exception e){
                    break;
                }
                //将buffer的position置为0，mark作废
                byteBuffer.rewind();
            }
        }

    }
}
